package com.carlinfo.userscenter.users.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.carlinfo.userscenter.common.dao.impl.BaseDaoImpl;
import com.carlinfo.userscenter.common.util.PageInfoUtil;
import com.carlinfo.userscenter.users.dao.IARoleDao;
import com.carlinfo.userscenter.users.pojo.ARole;

public class ARoleDaoImpl extends BaseDaoImpl<ARole> implements IARoleDao
{
	private ARole convertFromDb_Source(ResultSet rs) throws SQLException
	{
		int id = rs.getInt("id");
		String name = rs.getString("name");
		String content = rs.getString("content");
		byte status = rs.getByte("status");
		Date createTime = rs.getTimestamp("createTime");
		Date updateTime = rs.getTimestamp("updateTime");
		Date pubTime = rs.getTimestamp("pubTime");

		System.out.println("id:" + id + ";name:" + name + ";content:" + content + ";status:" + status + ";createTime:"
				+ createTime + ";updateTime:" + updateTime + ";pubTime:" + pubTime);
		ARole role = new ARole();
		role.setId(id);
		role.setId(id);
		role.setName(name);
		role.setContent(content);
		role.setStatus(status);
		role.setCreateTime(createTime);
		role.setUpdateTime(updateTime);
		role.setPubTime(pubTime);
		return role;
	}

	@Override
	public int saveOne(ARole role)
	{
		System.out.println("==ARoleDaoImpl==saveOne==");
		String sql = "insert into `a_role`(`name`, `content`, `status`, `createTime`, `updateTime`, `pubTime`) "
				+ "values "
				+ "(?,?,?,?,?,?)";
		List<Object> paramsList = new ArrayList<Object>();
		paramsList.add(role.getName());
		paramsList.add(role.getContent());
		paramsList.add(role.getStatus());
		paramsList.add(role.getCreateTime());
		paramsList.add(role.getUpdateTime());
		paramsList.add(role.getPubTime());
		
		int res =  this.executeSqlUpdate(sql, paramsList);
		role.setId( (int) paramsList.get(paramsList.size() - 1) );
		return res ;
	}

	public int updateOne(ARole role)
	{
		String sql = "update `a_role` set `name` = ?, " + "`content` = ?, `status` = ?, `createTime` = ?, "
				+ "`updateTime` = ?, `pubTime` = ? " + " where `id` = ?";

		List<Object> paramsList = new ArrayList<Object>();
		paramsList.add(role.getName());
		paramsList.add(role.getContent());
		paramsList.add(role.getStatus());
		paramsList.add(role.getCreateTime());
		paramsList.add(role.getUpdateTime());
		paramsList.add(role.getPubTime());
		paramsList.add(role.getId());

		return this.executeSqlUpdate(sql, paramsList);
	}

	public int deleteOne(ARole role)
	{
		String sql = "delete from a_role       " + "        where `id` = ?";
		System.out.println(sql);
		List<Object> paramsList = new ArrayList<Object>();
		paramsList.add(role.getId());
		return this.executeSqlUpdate(sql, paramsList);
	}

	@Override
	public ARole findOne(Map<String, Object> condMap)
	{
		List<Object> paramsList = new ArrayList<Object>();
		String sql = "select * from a_role ";
		if (condMap.get("id") != null && !"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sql += "where id = ?";
			paramsList.add(condMap.get("id"));
		} else if (condMap.get("name") != null && !"".equalsIgnoreCase(condMap.get("name") + ""))
		{
			sql += "where name = ?";
			paramsList.add(condMap.get("name"));
		}
		System.out.println("ARole==findOne==sql==" + sql);
		System.out.println("ARole==findOne==条件==" + paramsList);

		return this.findOneBase(sql, paramsList, new ARole());

	}

	@Override
	public List<ARole> findCondList(PageInfoUtil pageInfoUtil ,Map<String, Object> condMap)
	{
		System.out.println("==ARoleDaoImpl==findCondList==");
		List<ARole> roleList = new ArrayList<>();
		List<Object> paramList = new ArrayList<>();
		String sql = "select * from a_role where 1 = 1 ";
		if (condMap.get("keyword") != null && !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			sql = sql + " and (name like ? or content like ?)";
			paramList.add(condMap.get("keyword"));
			paramList.add(condMap.get("keyword"));
		}
		
		if (condMap.get("status") != null && !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			sql = sql + " and status = ?";
			paramList.add(condMap.get("status"));
		}
		
		if (condMap.get("stTime") != null && !"".equalsIgnoreCase(condMap.get("stTime") + ""))
		{
			sql = sql +  " and pubTime > ? and pubTime < ? ";
			paramList.add(condMap.get("stTime"));
			paramList.add(condMap.get("edTime"));
		}
		
		if("iddesc".equalsIgnoreCase(condMap.get("orderby") + ""))
		{
			sql = sql + "order by id desc ";
		}else if("idasc".equalsIgnoreCase(condMap.get("orderby") + ""))
		{
			sql = sql + "order by id asc ";
		}else 
		{
			sql = sql + "order by id desc ";
		}
		System.out.println("ARole==findOne==sql==" + sql);
		
		if(pageInfoUtil != null)
		{
			return this.findByPage(pageInfoUtil, sql, paramList, new ARole());
		}
			return this.find(sql, paramList, new ARole());
	}
	
}
